﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Equipment.Consum
{
    public partial class recipientsapplication : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            strPageUserType.Add("teacher");

            if (!Page.IsPostBack)
            {
                ddlLabs.Attributes.Add("onchange", "LabChange()");
                ddlEType.Attributes.Add("onchange", "LabChange()");
                tbRecipientsDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
                bind();
                tbEType.Style.Add("display", "none");
                tbCNub.Attributes.Add("readOnly", "-1");
                tbRecipientsDate.Attributes.Add("readOnly", "-1");
                lbCName.Attributes.Add("readOnly", "-1");
                lbTypeNub.Attributes.Add("readOnly", "-1");
                lbAllNub.Attributes.Add("readOnly", "-1");
                tbLabName.Attributes.Add("readOnly", "-1");
            }
        }

      
        void bind()
        {
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList(" Lab_ParentID is null ");
            ddlLabs.DataSource = mLab;
            ddlLabs.DataTextField = "Lab_Name";
            ddlLabs.DataValueField = "Lab_Code";
            ddlLabs.DataBind();

            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            
            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();

        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            
            #region 验证
            if (string.IsNullOrEmpty(tbCNub.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "耗材编号不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbUserNub.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用数量不能为空!");
                return;
            }
            string strTotalNub = lbAllNub.Text.Trim();
            decimal iTotalNub = 0;
            decimal.TryParse(strTotalNub, out iTotalNub);

            string strUseNub = tbUserNub.Text.Trim();
            decimal iUserNub = 0;
            decimal.TryParse(strUseNub, out iUserNub);

            if (iUserNub > iTotalNub)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "库存不足!");
                return;
            }

            if (string.IsNullOrEmpty(tbLabName.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用实验室不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbRecipientsDate.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用时间不能为空!");
                return;
            }

            #endregion
            lbErr.Visible = false;
            Button btn = (Button)sender;
            string strbtnName = btn.ID;
            LabMS.BLL.RecipientsApp brecipientsApp = new LabMS.BLL.RecipientsApp();
            LabMS.Model.RecipientsApp recipientsApp = new LabMS.Model.RecipientsApp();
            recipientsApp.State = "0";
            if (strbtnName == "Submit")
            {
                recipientsApp.State = "1";
                recipientsApp.AppTime = DateTime.Now;
            }
            recipientsApp.AppComment = Common.codehelper.Encode(Comment.Text.Trim());
            recipientsApp.AppNub = tbUserNub.Text.Trim();
            recipientsApp.Arrow = ddlArrow.SelectedValue;
            recipientsApp.ClassNumber = tbCNub.Text;
            recipientsApp.EncodedWarehouse = ddlLabs.SelectedValue;
            recipientsApp.Laboratory = tbLabCode.Value;
            recipientsApp.MRecipients = UserRealName;
            recipientsApp.Name = lbCName.Text;
            recipientsApp.Numbers = "";//编号不需要
            DateTime temp = new DateTime();
            if (DateTime.TryParse(tbRecipientsDate.Text, out temp))
            {
                recipientsApp.RecipientTime = temp;
            }
            else
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用时间格式错误!");
                return;
            }
            recipientsApp.Type = ddlEType.SelectedValue;
            recipientsApp.Words = lbTypeNub.Text;
            try
            {
                brecipientsApp.Add(recipientsApp);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "操作成功!", "recipientsapplication.aspx");
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
            }
        }
    }
}
